package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.MyFriendListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.MyFriend;
import com.isunland.managesystem.entity.MyFriendOriginal;
import com.isunland.managesystem.entity.SuccessMessage;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.SharedPreferencesUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONException;

/**
 * 对象功能:  我关注的专家列表
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/3/20 14:18
 */
public class MyAttentExpertListFragment extends BaseListFragment
    implements MyFriendListAdapter.Callback {
  private ArrayList<MyFriend> mList;
  private int REQUEST_MORE = 0;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.myFollow);
    mList = new ArrayList<>();
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_more, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_add:
        //专家列表: 资深用户和认证专家
        Intent expert = new Intent(getActivity(), ExpertPagerActivity.class);
        //startActivity(expert);
        startActivityForResult(expert, REQUEST_MORE);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    return ApiConst.URL_MY_EXPERTLIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("jobno", CurrentUser.newInstance(getActivity()).getJobNumber());
    params.put("subCode",
        SharedPreferencesUtil.getString(getActivity(), SharedPreferencesUtil.KEY_FORUM_SUBCODE,
            ""));
    return params;
  }

  @Override public void resolveResponse(String json) {
    MyFriendOriginal original = new Gson().fromJson(json, MyFriendOriginal.class);
    if (original.getResult() == Base.SUCCESS && original.getRows() != null) {
      //获取列表成功
      mList.clear();
      mList.addAll(original.getRows());
      MyFriendListAdapter mAdapter =
          new MyFriendListAdapter(getActivity(), mList, this, MyFriendListAdapter.EXPERT);
      setListAdapter(mAdapter);
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  //可以查看专家发布的帖子和回复
  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    MyFriend content = mList.get(position - 1);
    Intent intent = new Intent(getActivity(), AgriculturalMyExpertPagerActivity.class);
    intent.putExtra(AgriculturalForumListFragment.EXTRA_FROM, AgriculturalForumListFragment.EXPERT);
    intent.putExtra(AgriculturalForumListFragment.EXTRA_JOBNO, content.getToAddJobno());//专家jobno
    intent.putExtra(AgriculturalForumListFragment.EXTRA_NAME, content.getToAddName());//专家name
    startActivity(intent);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == REQUEST_MORE) {
      volleyPost();
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  @Override public void clickView(int position) {
    MyFriend content = mList.get(position);
    //取消关注
    volleyPostCancleAttent(content.getToAddJobno());
  }

  /* 取消关注 */
  private void volleyPostCancleAttent(String jobno) {
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_EXPERT_CANCLE_ATTENTION);
    HashMap<String, String> params = new HashMap<>();
    params.put("toAddJobno", jobno);
    MyUtils.showDialogLoading(getActivity(), "正在取消关注");
    mActivity.volleyPost(url, params, getResponse());
  }

  private VolleyResponse getResponse() {
    return new VolleyResponse() {
      @Override public void onVolleyError(VolleyError arg0) {

      }

      @Override public void onVolleyResponse(String json) throws JSONException {

        MyUtils.dismissDialog();
        try {
          SuccessMessage successsMessage = new Gson().fromJson(json, SuccessMessage.class);

          String successType = successsMessage.getResult();

          if (successType.equals("1")) {
            ToastUtil.show(R.string.success_operation);
            volleyPost();
          } else {
            ToastUtil.show(successsMessage.getMessage());
          }
        } catch (Exception e) {
          LogUtil.e("error", e);
          ToastUtil.show(R.string.failure_operation);
        }
      }
    };
  }
}
